home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / Word8Array.mlp < prev    next >
Encoding:
Text File  |  1996-07-03  |  4.4 KB  |  144 lines  |  [TEXT/R*ch]

  1. (* Word8Array -- as of 1994-12-21 *)
  2.  
  3. type elem   = Word8.word;
  4. type vector = Word8Vector.vector;
  5.  
  6. local 
  7.     prim_eqtype array_;
  8.     prim_val array_   : int -> array_                 = 1 "create_string";
  9.     prim_val vector_  : int -> vector                 = 1 "create_string";
  10.     prim_val sub_     : array_ -> int -> elem         = 2 "get_nth_char";
  11.     prim_val update_  : array_ -> int -> elem -> unit = 3 "set_nth_char";
  12.     prim_val length_  : array_ -> int                 = 1 "string_length";
  13.     prim_val lengthv_ : vector -> int                 = 1 "string_length";
  14.     prim_val fill_    : array_ -> int -> int -> elem -> unit 
  15.                                                       = 4 "fill_string";
  16.     prim_val blitaa_  : array_ -> int -> array_ -> int -> int -> unit 
  17.                                                       = 5 "blit_string";
  18.     prim_val blitav_  : array_ -> int -> vector -> int -> int -> unit 
  19.                                                       = 5 "blit_string";
  20.     prim_val blitva_  : vector -> int -> array_ -> int -> int -> unit 
  21.                                                       = 5 "blit_string";
  22. in 
  23.  
  24. type array = array_ ref;
  25.  
  26. #include "../config/m.h"
  27. #ifdef SIXTYFOUR
  28. val maxLen = 144115188075855863;      (* = (2^54-1)*8-1, with 64 bit *)
  29. #else
  30. val maxLen = 16777211;          (* = (2^22-1)*4-1, with 32 bit *)
  31. #endif
  32.  
  33. val array0 = ref (array_ 0);
  34.  
  35. fun array(n, v: elem) =
  36.     let val a = if n < 0 orelse n > maxLen then raise Size else array_ n 
  37.     in fill_ a 0 n v; ref a end;
  38.  
  39. fun tabulate(n, f : int -> elem) =
  40.   if n < 0 orelse n > maxLen then raise Size else
  41.   let val a = array_ n
  42.       fun init i = if i >= n then () else (update_ a i (f i); init (i+1))
  43.   in init 0; ref a end;
  44.  
  45. fun fromList (vs : elem list) =
  46.     let val n = List.length vs
  47.     val a = if n > maxLen then raise Size else array_ n 
  48.     fun init [] i = ()
  49.       | init (v::vs) i = (update_ a i v; init vs (i+1))
  50.     in init vs 0; ref a end;
  51.  
  52. fun length (ref a) = length_ a;
  53.  
  54. fun sub(ref a, i) =
  55.   if i < 0 orelse i >= length_ a then raise Subscript 
  56.   else sub_ a i;
  57.  
  58. fun update(ref a, i, v) =
  59.   if i < 0 orelse i >= length_ a then raise Subscript 
  60.   else update_ a i v;
  61.  
  62. fun extract (ref a, i, slicelen) =
  63.     let val n = case slicelen of NONE => length_ a - i | SOME n => n 
  64.     val newvec = if i<0 orelse n<0 orelse i+n > length_ a then
  65.                          raise Subscript
  66.              else
  67.                  vector_ n 
  68.     in blitav_ a i newvec 0 n; newvec end;
  69.  
  70. fun copy {src = ref a1: array, si=i1, dst = ref a2: array, di=i2, len=n} =
  71.     if n<0 orelse i1<0 orelse i1+n > length_ a1
  72.            orelse i2<0 orelse i2+n > length_ a2
  73.     then raise Subscript
  74.     else blitaa_ a1 i1 a2 i2 n;
  75.  
  76. fun copyv {src = a1: vector, si=i1, dst = ref a2: array, di=i2, len=n} =
  77.     if n<0 orelse i1<0 orelse i1+n > lengthv_ a1
  78.            orelse i2<0 orelse i2+n > length_ a2
  79.     then raise Subscript
  80.     else blitva_ a1 i1 a2 i2 n;
  81.  
  82. fun foldl f e (ref a) = 
  83.     let val stop = length_ a
  84.     fun lr j res = if j < stop then lr (j+1) (f(sub_ a j, res))
  85.                else res
  86.     in lr 0 e end
  87.  
  88. fun foldr f e (ref a) =
  89.     let fun rl j res = if j >= 0 then rl (j-1) (f(sub_ a j, res))
  90.                else res
  91.     in rl (length_ a - 1) e end
  92.  
  93. fun modify f (ref a) = 
  94.     let val stop = length_ a
  95.     fun lr j = if j < stop then (update_ a j (f(sub_ a j)); lr (j+1))
  96.            else ()
  97.     in lr 0 end
  98.  
  99. fun app f (ref a) = 
  100.     let val stop = length_ a
  101.     fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
  102.            else ()
  103.     in lr 0 end
  104.  
  105. fun sliceend (a, i, NONE) = 
  106.         if i<0 orelse i>length a then raise Subscript
  107.     else length a
  108.   | sliceend (a, i, SOME n) = 
  109.     if i<0 orelse n<0 orelse i+n>length a then raise Subscript
  110.     else i+n;
  111.  
  112. fun foldli f e (slice as (ref a, i, _)) = 
  113.     let fun loop stop =
  114.         let fun lr j res = 
  115.         if j < stop then lr (j+1) (f(j, sub_ a j, res))
  116.         else res
  117.         in lr i e end
  118.     in loop (sliceend slice) end;
  119.  
  120. fun foldri f e (slice as (ref a, i, _)) = 
  121.     let fun loop start =
  122.         let fun rl j res = 
  123.             if j >= i then rl (j-1) (f(j, sub_ a j, res))
  124.             else res
  125.         in rl start e end;
  126.     in loop (sliceend slice - 1) end
  127.  
  128. fun modifyi f (slice as (ref a, i, _)) = 
  129.     let fun loop stop =
  130.         let fun lr j = 
  131.         if j < stop then (update_ a j (f(j, sub_ a j)); lr (j+1))
  132.         else ()
  133.         in lr i end
  134.     in loop (sliceend slice) end;
  135.  
  136. fun appi f (slice as (ref a, i, _)) = 
  137.     let fun loop stop = 
  138.         let    fun lr j = 
  139.             if j < stop then (f(j, sub_ a j); lr (j+1)) 
  140.             else ()
  141.         in lr i end
  142.     in loop (sliceend slice) end;
  143. end
  144.